package com.chen.controller;

import com.chen.pojo.Loginlog;
import com.chen.pojo.Loginout;
import com.chen.pojo.User;
import com.chen.service.*;
import com.chen.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.cert.TrustAnchor;
import java.util.Date;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    LoginlogService loginlogService;
    @Autowired
    LoginoutService loginoutService;

    @RequestMapping("/toUpdatePassword.html")
    public String toUpDatePassword(HttpServletRequest request){
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
        if(role==1){
            return "/WEB-INF/pages/admin/updatePassword";
        }if(role==2){
            return "/WEB-INF/pages/teacher/updatePassword";
        }else{
            return "/WEB-INF/pages/student/updatePassword";
        }

    }
    @RequestMapping("Adminleft.html")
    public String toAdminLeft(){
        return "/WEB-INF/pages/admin/left";
    }
    @RequestMapping("teacherLeft.html")
    public String toTeacherLeft(){
        return "/WEB-INF/pages/teacher/left";
    }
    @RequestMapping("studentLeft.html")
    public String toStudentLeft(){
        return "/WEB-INF/pages/student/left";
    }

    @RequestMapping("go_login.html")
    public String goLogin() {
        return "login";
    }

    @RequestMapping("login.html")
    public String login(User user, HttpSession session, Model model,HttpServletRequest request) {

        User user1 = null;
        try {
            if (user.getRole() == 1) {
                user1 = userService.getAdminByUserName(user.getUsername());
                if(user1!=null){
                    user1.setRole(1);
                }
            }
            if(user.getRole()==2){
                user1 = userService.getTeacherByUserName(user.getUsername());
                if(user1!=null){
                    user1.setRole(2);
                }

            }
            if(user.getRole()==3){
                user1=userService.getStudentByUserName(user.getUsername());
                if(user1!=null){
                    user1.setRole(3);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
            return "login";
        }


        if (user1 == null || !user1.getPassword().equals(user.getPassword())) {
            //用户名或密码错误
            model.addAttribute("error", "用户名或密码错误");
//            return  "redirect:go_login.html";
            return "login";
        }

        user1.setImgUrl(Constants.IMG_URL+user1.getImgUrl());
        session.setAttribute(Constants.SESSION_USER, user1);
        session.setAttribute(Constants.USER_TYPE, user1.getRole());
        System.out.println(user1);
        Loginlog loginlog = new Loginlog();
        loginlog.setUsername(user1.getUsername());
        loginlog.setName(user1.getName());
        loginlog.setLoginDate(new Date());
        loginlog.setRole(user1.getRole());
        try {
            loginlogService.addLoginlog(loginlog);
            System.out.println("记录成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        if(user.getRole() ==1){
            return "/WEB-INF/pages/admin/index";
        }
        if(user.getRole()==2){
            return "/WEB-INF/pages/teacher/index";
        }
        else{
            return "/WEB-INF/pages/student/index";
        }
    }


    @RequestMapping("out.html")
    public String out(HttpSession session){
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        Loginout loginout = new Loginout();
        loginout.setUsername(user.getUsername());
        loginout.setName(user.getName());
        loginout.setLoginOutDate(new Date());
        loginout.setRole(user.getRole());
        System.out.println("添加注销日志");
        try {
            loginoutService.addLoginout(loginout);
            System.out.println("添加成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        session.invalidate();
        return "login";
    }

    @RequestMapping("error.html")
    public String error(){
        return "/error";
    }

    @RequestMapping("updatePassword.html")
    public String updatePassword(String password,HttpSession session, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(Constants.SESSION_USER);
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
        user.setPassword(password);
        if(role==1){
            userService.updatePassword(user);
        }
        if(role==3){
            studentService.updatePassword(user);
        }
        if(role==2){
            teacherService.updatePassword(user);
        }

        session.setAttribute("mess","修改成功");
        return "main";
    }
}
